/**
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gitee.huangjianbo.supos.openapi.core.http;

import java.util.Arrays;
import java.util.Locale;

import io.gitee.huangjianbo.supos.openapi.core.errors.NotSupportLanguageException;

/**
 * 请求语言
 *
 * @author tomcat
 * @version 1.0
 */
public enum HttpLanguage {
    /**
     * 简体中文
     */
    zh_CN("zh-cn"),
    /**
     * 繁体中文
     */
    zh_HK("zh-hk"),
    /**
     * 英文（美式）
     */
    en_US("en-us"),
    /**
     * 法语
     */
    fr_FR("fr-fr"),
    /**
     * 德语
     */
    de_DE("de-de"),
    /**
     * 意大利语
     */
    it_IT("it-it"),
    /**
     * 西班牙语
     */
    es_ES("es-es"),
    /**
     * 葡萄牙语
     */
    pt_PT("pt-pt"),
    /**
     * 俄语
     */
    ru_RU("ru-ru"),
    /**
     * 荷兰语
     */
    nl_NL("nl-nl"),
    /**
     * 瑞典语
     */
    sv_SE("sv_SE"),
    /**
     * 丹麦语
     */
    da_DK("da-dk"),
    /**
     * 芬兰语
     */
    fi_FI("fi-fi"),
    /**
     * 希腊语
     */
    el_GR("el-gr"),
    /**
     * 波兰语
     */
    pl_PL("pl-pl"),
    /**
     * 罗马尼亚语
     */
    ro_RO("ro-ro"),
    /**
     * 土耳其语
     */
    tr_TR("tr-tr"),
    /**
     * 日文
     */
    ja_JP("ja-jp"),
    /**
     * 韩语
     */
    ko_KR("ko-kr"),
    /**
     * 阿拉伯语-阿拉伯联合酋长国
     */
    ar_AE("ar-ae"),
    /**
     * 阿拉伯语-沙特阿拉伯
     */
    ar_SA("ar-sa"),
    /**
     * 泰语
     */
    th_TH("th-th"),
    /**
     * 印度尼西亚语
     */
    id_ID("id-id"),
    /**
     * 马来语
     */
    ms_MY("ms-my"),
    /**
     * 菲律宾语
     */
    tl_PH("tl-ph"),
    /**
     * 乌尔都语-巴基斯坦
     */
    ur_PK("ur-pk"),
    /**
     * 乌尔都语-印度
     */
    ur_IN("ur-in");

    /**
     * Accept-Language字段值
     */
    private final String acceptLanguage;

    HttpLanguage(String acceptLanguage) {
        this.acceptLanguage = acceptLanguage;
    }

    /**
     * 将当前语言转换为{@link HttpLanguage}枚举值
     * 
     * @param locale 当前语言
     * @return
     */
    public static HttpLanguage of(Locale locale) {
        return HttpLanguage.valueOf(locale.getLanguage().toLowerCase() + "_" + locale.getCountry().toUpperCase());
    }

    /**
     * 将Accept-Language字段值转换为{@link HttpLanguage}枚举值
     * 
     * @param acceptLanguage HTTP请求头中的Accept-Language字段值
     * @return
     */
    public static HttpLanguage of(String acceptLanguage) {
        return Arrays.stream(HttpLanguage.values())
                .filter(l -> l.getAcceptLanguage().equals(acceptLanguage.toLowerCase()))
                .findFirst()
                .orElseThrow(() -> new NotSupportLanguageException(acceptLanguage));
    }

    public String getAcceptLanguage() {
        return acceptLanguage;
    }
}
